<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Collision detection</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>

<body>

<div align="center">
<table class=allEncompassingTable >
 <tr>
  <td >
<p><a href="../index.html" TARGET="_top"><img src="images/homeImg.png"></a></p>



<h1>Collision detection</h1>


<p>CoppeliaSim can detect collisions between two <a href="collidableObjects.htm">collidable</a> <a href="entities.htm">entities</a> in a flexible way. The calculation is an exact interference calculation. Collision detection, as its name states, only detect collisions; it does however not directly react to them (for collision response, refer to the <a href="dynamicsModule.htm">dynamics module</a>).<br>
</p>

<p align=center><img src="images/collisionDetection1.jpg"></p>
<p class=imageLabel>[Collision detection between two manipulators]</p>
<br>

<p>Using the API function <a href="regularApi/simCheckCollision.htm">sim.checkCollision</a>, one can easily detect collision between entities, for instance collision detection between a robot and its environment, from within a <a href="childScripts.htm">child script</a>, in each simulation step:</p>

<pre class=lightRedBox>
function sysCall_init()
    local robotBase=sim.getObjectHandle('/robotModelAlias')
    robotCollection=sim.createCollection(0)
    sim.addItemToCollection(robotCollection,sim.handle_tree,robotBase,0)
end

function sysCall_sensing()
    local result,pairHandles=sim.checkCollision(robotCollection,sim.handle_all)
    if result&gt;0 then
        print('Robot is colliding. Colliding pair is '..getAsString(pairHandles))
    end
end</pre>

<p>One can also temporarily modify the color of <a href="objects.htm">objects</a> or whole <a href="collections.htm">collections</a>, in order to visually indicate a collision:</p>

<pre class=lightRedBox>
function changePairColor(entityPair,colorPair)
    originalColorData={}
    originalColorData[1]=sim.changeEntityColor(entityPair[1],colorPair[1])
    originalColorData[2]=sim.changeEntityColor(entityPair[2],colorPair[2])
end

function restorePairColor()
    if originalColorData then
        sim.restoreEntityColor(originalColorData[1])
        sim.restoreEntityColor(originalColorData[2])
        originalColorData=nil
    end
end

function sysCall_init()
    local robotBase=sim.getObjectHandle('/irb360')
    robotCollection=sim.createCollection(0)
    sim.addItemToCollection(robotCollection,sim.handle_tree,robotBase,0)
    collisionColors={{1,0,0},{1,0,1}} -- collider and collidee
end

function sysCall_sensing()
    local result,pairHandles=sim.checkCollision(robotCollection,sim.handle_all)
    restorePairColor()
    if result&gt;0 then
        -- Change color of the collection and the collidee:
        changePairColor({robotCollection,pairHandles[2]},collisionColors)
        -- Change color of the collider and collidee objects:
        -- changePairColor({pairHandles[1],pairHandles[2]},collisionColors)
    end
end

function sysCall_cleanup()
    restorePairColor()
end</pre>



<p>CoppeliaSim's collision detection functionality is also available as stand-alone routines via the <a href="coppeliaGeometricRoutines.htm">Coppelia geometric routines</a>.</p>


<br>
<br>

  </tr>
</table> 
</div>  
  
  
</body>

</html>
